package com.ifreshwing.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ifreshwing.activity.base.LoginActivity;
import com.ifreshwing.activity.mine.CopyrightStatementActivity;
import com.ifreshwing.activity.mine.ModifyPasswordActivity;
import com.ifreshwing.userpark.R;
import com.ifreshwing.utils.SharedPreferencesUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by lz on 2017/4/28.
 */

public class MineFragment extends Fragment {
    @BindView(R.id.titleBar_text)
    TextView titleBarText;
    @BindView(R.id.rl_modify_pwd)
    RelativeLayout rlModifyPwd;
    @BindView(R.id.rl_copyright)
    RelativeLayout rlCopyright;
    @BindView(R.id.ll_logout)
    LinearLayout llLogout;
    Unbinder unbinder;
    private View view;
    Context context;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_mine, null, false);
        unbinder = ButterKnife.bind(this, view);
        titleBarText.setText("我的");
        context = getActivity();
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.rl_modify_pwd, R.id.rl_copyright, R.id.ll_logout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_modify_pwd:
                startActivity(new Intent(getActivity(), ModifyPasswordActivity.class));
                break;
            case R.id.rl_copyright:
                startActivity(new Intent(getActivity(), CopyrightStatementActivity.class));
                break;
            case R.id.ll_logout:
                showDialog();
                break;
        }
    }

    private void showDialog() {
        final AlertDialog.Builder dialog = new AlertDialog.Builder(context);
        dialog.setTitle("提示").
                setMessage("确定要退出登录吗?").
                setPositiveButton("确定", (dialog1, which) -> {
                    SharedPreferencesUtil.setIsLogin(context, false);
                    //此处做退出登录清理缓存信息
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                    getActivity().finish();
                }).setNegativeButton("取消", (dialog12, which) -> {
        }).show();
    }
}
